<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=D6WOzHaymzVVKvgiy8UbhQEznkgeK6BD"></script>
    <script src="./jquery.min.js"></script>
    <title>Document</title>
</head>
<body>
    <div>999</div>
    
  <div id="msg"></div>

<script type="text/javascript">
    //百度地图获取坐标
    function getAddr() {
        var geolocation = new BMap.Geolocation();
        var pt;
        geolocation.getCurrentPosition(function (r) {
            if (this.getStatus() == BMAP_STATUS_SUCCESS) {
            console.log('rrrrrr',r)

                //setCookie("localX", r.point.lng);
                //setCookie("localY", r.point.lat);
                // alert(r.point.lng + " ， " + r.point.lat);
                pt = r;
                // 121.48789949 ， 31.24916171
                showPosition(pt);
                //传参数给地图按钮
                //document.getElementById("alinkMap").href += "&x="+r.point.lng+"&y="+r.point.lat;
                //document.getElementById("alinkMap").style.display="block";
            }
        });
    }
    //百度地图WebAPI 坐标转地址
    function showPosition(r) {
        console.log('r',r)
        // ak = appkey 访问次数流量有限制
        var url = 'http://api.map.baidu.com/geocoder/v2/?ak=D6WOzHaymzVVKvgiy8UbhQEznkgeK6BD&callback=?&location=' + r.point.lat + ',' + r.point.lng + '&output=json&pois=1';
        $.getJSON(url, function (res) {
            console.log('res',res)
            $("#msg").html(url);
            // alert(res.result.addressComponent.city);
            getLocation()
        });
    }
    //百度地图JS API 坐标转地址，没有加载地图时获取不到rs,总是null
    function getLocation(pt,rs) {
        // 根据坐标得到地址描述 
        var map = new BMap.Map("allmap");
            var point = new BMap.Point(121.574994,31.301825);
            // var point = new BMap.Point(121.575082, 31.302039  );
            var gc = new BMap.Geocoder();
            gc.getLocation(point, function (res) {
                console.log('resaaaaa',res)
            if (rs) {
                var addComp = rs.addressComponents;
                window.clearInterval(interval);
                alert(addComp);
            }
            return rs;
        });
    }
    var gpsPoint = new BMap.Point(121.574994, 31.301825);
    console.log('gpsPoint',BMap.Convertor)
                BMap.Convertor.translate(gpsPoint, 0, function (point) {
                    var geoc = new BMap.Geocoder();
                    geoc.getLocation(point, function (rs) {
                        var addComp = rs.addressComponents;
                        alert(addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber);
                    });
                });

    getAddr()
    </script>
</body>

</html>
